# TODO add NULL

class IBMModel1:
    def __init__(self, le, lf):
        self.le = le
        self.lf = lf

        self.t = []     # index: e, f
        for i in range(le):
            self.t.append([])
            for j in range(lf):
                self.t[i].append(1 / lf)

    def train(self, pairs):
        # temp variables
        s_total = [0 for _ in range(self.le)]
        total = [0 for _ in range(self.lf)]
        count = []
        for e in range(self.le):
            count.append([])
            for f in range(self.lf):
                count[e].append([0])

        # reset
        for i in range(len(total)):
            total[i] = 0
        for i in range(len(count)):
            for j in range(len(count[i])):
                count[i][j] = 0

        for src_sent, tgt_sent in pairs:
            # reset
            for i in range(len(s_total)):
                    s_total[i] = 0

            # calc s_total
            for e in tgt_sent:
                for f in src_sent:
                    s_total[e] += self.t[e][f]

            # calc total and count
            for e in tgt_sent:
                for f in src_sent:
                    count[e][f] += self.t[e][f] / s_total[e]
                    total[f] += self.t[e][f] / s_total[e]

        for e in range(self.le):
            for f in range(self.lf):
                # update t
                self.t[e][f] = count[e][f] / total[f]
